import javax.xml.bind.JAXBException;

/**
 * 
 * @author ryannikolaidis
 *
 * This is where performance events are scheduled.
 *
 */

public class Follow implements Runnable {
	public Object[] oscArray = new Object[2];
	public Object[] noteArray = new Object[3];
	
	public void run() 
	{	
		while(true)
		{
			double wait = .3;
			
			if (Main.score.mallet0)
			{
				noteOut(Main.score.malletLoc0, 120,1);
			}
			if (Main.score.mallet1)
			{
				noteOut(Main.score.malletLoc1-7, 120,2);
			}
			wait(wait);
			if (Main.score.mallet0)
			{				
				noteOut(Main.score.malletLoc0-5, 120,0);
			}
			if (Main.score.mallet1)
			{
				noteOut(Main.score.malletLoc1, 120,3);
			}
			wait(wait);

			if (Main.score.mallet0)
			{
				noteOut(Main.score.malletLoc0-2, 120,1);
			}
			if (Main.score.mallet1)
			{				
				noteOut(Main.score.malletLoc1-5, 120,2);
			}
			wait(wait);
			if (Main.score.mallet0)
			{
				noteOut(Main.score.malletLoc0-7, 120,0);
			}
			if (Main.score.mallet1)
			{
				noteOut(Main.score.malletLoc1-2, 120,3);
			}

			
			wait(wait);
//			System.out.println("test");
		}
	}
	
	
	public void wait(double wait) 
	{
		long startTime=System.nanoTime();
		long triggerTime=startTime+(long)(1000000*wait*Main.quarter);
		long currentTime=System.nanoTime();
		
		while (currentTime<triggerTime)
		{
			triggerTime=startTime+(long)(1000000*wait*Main.quarter);
			currentTime=System.nanoTime();
		}
	}
	public void noteOut(int note, double velocity,int arm){
		int vel = (int) (velocity*Main.velocity/100.);
		noteArray[0] = arm;
		noteArray[1] = note;
		noteArray[2] = vel;
		
		OSC.main(noteArray,"/directControl");
		int[] intNoteArray=new int[3];
		intNoteArray[0]=(Integer) noteArray[0];
		intNoteArray[1]=(Integer) noteArray[1];
		System.out.println("note out: "+noteArray[1]);
	}
	public void noteOut2(int note, double velocity,double wait){
		int vel = (int) (velocity*Main.velocity/100.);
//		noteArray[0] = arm;
		noteArray[0] = note;
		noteArray[1] = vel;
		
		OSC.main(noteArray,"/mididata");
		int[] intNoteArray=new int[3];
		intNoteArray[0]=(Integer) noteArray[0];
		intNoteArray[1]=(Integer) noteArray[1];
		System.out.println("note out: "+noteArray[1]);
		wait(wait);
	}
	


}



